1/* <The House that Stief Built>, by <Aryeh Stiefel>. */
    2
    3:- dynamic i_am_at/1, at/2, holding/1.    4:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(holding(_)).    5
    6i_am_at(outside_stadium).
    7
    8/* Describes the connections between the rooms */
    9path(outside_stadium, n, ticket_booth).
   10path(outside_stadium, w, gate6).
   11
   12path(ticket_booth, s, outside_stadium).
   13
   14path(gate6, e, outside_stadium).
   15path(gate6, n, m_concourse) :- holding(ticket).
   16path(gate6, n, m_concourse) :- 
   17    write('You can not enter without a ticket'), nl, !, fail.
   18
   19path(m_concourse, w, bathroom).
   20path(m_concourse, n, seats1).
   21path(m_concourse, e, escalator1).
   22
   23path(bathroom, e, m_concourse).
   24path(bathroom, w, atm).
   25
   26path(atm, e, bathroom).
   27
   28path(seats1, s, m_concourse).
   29
   30path(escalator1, n, s_concourse).
   31path(escalator1, w, m_concourse).
   32
   33path(s_concourse, s, escalator1).
   34path(s_concourse, n, seats2).
   35path(s_concourse, w, concessions) :- holding(money).
   36path(s_concourse, w, concessions) :- 
   37    write('You need to get money before you can enter the concession stand'), nl, !, fail.
   38path(s_concourse, e, escalator2).
   39
   40path(seats2, s, s_concourse).
   41
   42path(concessions, e, s_concourse).
   43
   44path(escalator2, w, s_concourse).
   45path(escalator2, n, seats3) :- holding(popcorn).
   46path(escalator2, n, seats3) :- write('You need to get popcorn '),
   47    write('before sitting down'), nl, !, fail.
   48path(escalator2, e, emergency_exit).
   49
   50
   51at(ticket, ticket_booth).
   52at(money, atm).
   53at(popcorn, concessions).
   54
   55
   56/* These rules describe how to pick up an object. */
   57
   58take(X) :-
   59        holding(X),
   60        write('You are already holding it!'),
   61        !, nl.
   62
   63take(X) :-
   64        i_am_at(Place),
   65        at(X, Place),
   66        retract(at(X, Place)),
   67        assert(holding(X)),
   68        write('OK.'),
   69        !, nl.
   70
   71take(_) :-
   72        write('I do not see it here.'),
   73        nl.
   74
   75
   76/* These rules describe how to put down an object. */
   77
   78drop(X) :-
   79        holding(X),
   80        i_am_at(Place),
   81        retract(holding(X)),
   82        assert(at(X, Place)),
   83        write('OK.'),
   84        !, nl.
   85
   86drop(_) :-
   87        write('You are not holding it!'),
   88        nl.
   89
   90i :- inventory.
   91
   92inventory :- holding(X), write('You are holding '), write(X), nl,
   93    fail.
   94
   95inventory :- \+holding(_), write('You are not holding anything'), nl, fail.
   96
   97inventory.
   98
   99/* These rules define the direction letters as calls to go/1. */
  100
  101n :- go(n).
  102
  103s :- go(s).
  104
  105e :- go(e).
  106
  107w :- go(w).
  108
  109
  110/* This rule tells how to move in a given direction. */
  111
  112go(Direction) :-
  113        i_am_at(Here),
  114        path(Here, Direction, There),
  115        retract(i_am_at(Here)),
  116        assert(i_am_at(There)),
  117        !, look.
  118
  119go(_) :-
  120        write('You can''t go that way.').
  121
  122
  123/* This rule tells how to look about you. */
  124
  125look :-
  126        i_am_at(Place),
  127        describe(Place),
  128        nl,
  129        notice_objects_at(Place),
  130        nl.
  131
  132
  133/* These rules set up a loop to mention all the objects
  134   in your vicinity. */
  135
  136notice_objects_at(Place) :-
  137        at(X, Place),
  138        write('There is a '), write(X), write(' here.'), nl,
  139        fail.
  140
  141notice_objects_at(_).
  142
  143
  144/* This rule tells how to die. */
  145
  146die :-
  147        !, finish.
  148
  149
  150/* Under UNIX, the "halt." command quits Prolog but does not
  151   remove the output window. On a PC, however, the window
  152   disappears before the final output can be seen. Hence this
  153   routine requests the user to perform the final "halt." */
  154
  155finish :-
  156        nl,
  157        write('The game is over. Please enter the halt. command.'),
  158        nl.
  159
  160
  161/* This rule just writes out game instructions. */
  162
  163instructions :-
  164        nl,
  165        write('Enter commands using standard Prolog syntax.'), nl,
  166        write('Available commands are:'), nl,
  167        write('start.             -- to start the game.'), nl,
  168        write('n.  s.  e.  w.     -- to go in that direction.'), nl,
  169        write('take(Object).      -- to pick up an object.'), nl,
  170        write('drop(Object).      -- to put down an object.'), nl,
  171        write('look.              -- to look around you again.'), nl,
  172        write('instructions.      -- to see this message again.'), nl,
  173        write('halt.              -- to end the game and quit.'), nl,
  174        write('i.                 -- to check your inventory.'), nl,
  175        nl.
  176
  177
  178/* This rule prints out instructions and tells where you are. */
  179
  180start :-
  181        instructions,
  182        look.
  183
  184
  185/* These rules describe the various rooms.  Depending on
  186   circumstances, a room may have more than one description. */
  187
  188describe(outside_stadium) :-
  189    write('You are outside the stadium'), nl,
  190    write('To the north is the ticket booth.'), nl,
  191    write('To the west is the entrance to the stadium.'), nl.
  192
  193describe(ticket_booth) :-
  194    write('You are in the ticket booth.'), nl,
  195    write('Purchase your tickets here.'), nl,
  196    write('Then head south back outside the stadium.'), nl.
  197
  198describe(gate6) :- 
  199    write('You are outside of Gate 6,'), nl,
  200    write('the main entrance to the stadium.'), nl,
  201    write('Go north to enter through the gate,'), nl,
  202    write('into the main concourse.'), nl,
  203    write('To the east is back outside the stadium'), nl.
  204
  205describe(m_concourse) :- 
  206    write('You are now in the main concourse.'), nl,
  207    write('To the west are the bathrooms.'), nl,
  208    write('To the north are seats.'), nl,
  209    write('To the east is an escalator.'), nl.
  210
  211describe(bathroom) :-
  212    write('Do you need to use the facilities?'), nl,
  213    write('You are now in the bathroom.'), nl,
  214    write('To the west is the atm and'), nl,
  215    write('to the east is the main concourse.'), nl.
  216
  217describe(atm) :-
  218    write('Welcome to the atm machine at the stadium,'), nl,
  219    write('Here you can take out money if you need'), nl,
  220    write('To the east you can head back to the bathroom'), nl.
  221
  222describe(seats1) :- 
  223    write('These are not your seats!'), nl,
  224    write('Go back south to the main concourse.'), nl.
  225
  226describe(escalator1) :-
  227    write('You just got off the escalator.'), nl,
  228    write('To the north is the small concourse.'), nl,
  229    write('To the west is the main concourse again'), nl.
  230
  231describe(s_concourse) :- 
  232    write('You are in the small concourse.'), nl,
  233    write('To the north are more seats.'), nl,
  234    write('To the west are concession stands.'), nl,
  235    write('To the south is the first escalator'), nl,
  236    write('Going east will bring you up the second escalator.'), nl.
  237
  238describe(seats2) :- 
  239    write('These are not your seats!'), nl,
  240    write('Go back south to the small concourse.'), nl.
  241
  242describe(concessions) :-
  243    holding(money),
  244    write('You are at the concession stands where'), nl,
  245    write('you can buy popcorn to enjoy during the game.'), nl,
  246    write('Going east will bring you back to the small concourse.'), nl.
  247
  248describe(escalator2) :-
  249    write('You just got off the second escalator.'), nl,
  250    write('To the north is another section of seats.'), nl,
  251    write('To the east is an emergency exit'), nl,
  252    write('Going back west will bring you back to the small concourse'), nl.
  253
  254describe(emergency_exit) :-
  255    write('You just exited the stadium.'), nl,
  256    write('You lose'), nl,
  257    die.
  258
  259describe(seats3) :-
  260    holding(popcorn),
  261    write('You have arrived at your seats'), nl,
  262    write('Congrats! You win!'), nl,
  263    finish, !.
  264
  265describe(seats3) :-
  266    write('These are your seats but'), nl,
  267    write('you do not have your popcorn'), nl,
  268    write('Go south to get back to the third escalator'), 
  269    nl